package com.itheima.d01_exam.test07;

import java.io.*;
import java.util.concurrent.*;

public class Test {

    /*
        使用匿名内部类方式开启线程，
        并完成文件的复制.
        记录执行时间.
            要求：
                1、在当前项目的根目录放置一个有内容的a.txt文件.(io流的相对目录在项目根路径)
                2、开线程的方式是匿名内部类方式.
                3、使用字符缓冲流对象一次读写一行的方式来复制文本文件.
                4、计算复制文件耗费的时间.
     */
    public static void main(String[] args) {
        //创建线程池：复用线程
        ExecutorService pool = new ThreadPoolExecutor(
                1,
                1,
                0,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(1),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        pool.execute(() -> {
            System.out.println("拷贝文件... ...");
            try (
                    FileInputStream fis = new FileInputStream("D:\\playerSort.txt");
                    FileOutputStream fos = new FileOutputStream("D:\\playerSort2.txt");
                    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
            ) {
                long start = System.currentTimeMillis();
                String line;
                while( (line = br.readLine()) != null ){
                    bw.write( line );
                    bw.newLine();
                }
                bw.flush();
                System.out.println("复制完毕...");
                long end = System.currentTimeMillis();
                System.out.println("耗时：" + (end - start));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
}
















